package com.woniuxy.aopdemo1;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import java.lang.reflect.Method;

public class MySpect implements MethodInterceptor {
    //定义通知
    public void checkPermission(){
        System.out.println("开启权限检查");
    }
    //定义后置通知
    public void commitThransaction(){
        System.out.println("模拟事务提交");
    }
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        checkPermission();
        //对方法名开头是add的方法进行增强
        if(invocation.getMethod().getName().startsWith("add")){
            System.out.println("新增方法!");
        }
        //核心还是反射
        //执行切入点的方法
        Object proceed = invocation.proceed();
        commitThransaction();
        return proceed;
    }
}
